package com.mobilesales.android.infrastructure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.mobilesales.android.R;
import com.mobilesales.android.util.MessageBox;

public class MapOverlays {

	private Context context;

	private GoogleMap map;

	// ArrayList<Marker> listItem = new ArrayList<Marker>();
	Marker currentLocationItem = null;
	Marker currentItem = null;

	Map<Marker, Boolean> visibilityMap = new HashMap<Marker, Boolean>();

	public MapOverlays(Context _context) {
		context = _context;
	}

	public void setMaps(GoogleMap _gmap) {
		map = _gmap;
		map.setOnMarkerClickListener(new OnMarkerClickListener() {
			public boolean onMarkerClick(Marker arg0) {
				// TODO Auto-generated method stub
				onTap(arg0);
				return false;
			}
		});
	}

	/**
	 * Them moi 1 dia diem va ban do
	 * 
	 * @param marker
	 */
	public void addItem(MarkerOptions marker) {
		Marker mk = map.addMarker(marker);
		// listItem.add(mk);
		visibilityMap.put(mk, true);
	}

	/**
	 * 
	 * @param latlng
	 * @param description
	 */
	public void addItem(LatLng latlng, String description) {
		MarkerOptions marker = new MarkerOptions();
		marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_blue));
		marker.title(description);
		marker.position(latlng);
		Marker mk = map.addMarker(marker);
		// listItem.add(mk);
		visibilityMap.put(mk, true);
	}

	/**
	 * 
	 * @param item
	 */
	public void setCurrentItem(Marker mr) {
		if (mr != null) {
			// Marker mr = getMarkerFromLatLng(item.getPosition());
			if (visibilityMap.containsKey(mr)) {
				if (currentItem != null) {
					if (visibilityMap.get(mr))
						currentItem.setIcon(BitmapDescriptorFactory
								.fromResource(R.drawable.pin_blue));
				}
			}
			currentItem = mr;
			mr.setIcon(BitmapDescriptorFactory
					.fromResource(R.drawable.pin_yellow));
		}
	}

	/**
	 * 
	 * @param latlng
	 */
	/*
	 * public void setCurrentItem(LatLng latlng) { Marker mr =
	 * getMarkerFromLatLng(latlng); setCurrentItem(mr); }
	 */

	/**
	 * 
	 * @param marker
	 */
	private final void onTap(Marker mr) {/*
										 * LatLng latlng = marker.getPosition();
										 * Marker mr =
										 * getMarkerFromLatLng(latlng);
										 */
		if (mr.equals(currentLocationItem)) {
			currentItem = null;
		} else if (visibilityMap.containsKey(mr)) {
			boolean isVisible = visibilityMap.get(mr);
			if (isVisible) {
				setCurrentItem(mr);
				map.moveCamera(CameraUpdateFactory.newLatLng(mr.getPosition()));
			}
		}
	}

	/**
	 * Lay 1 dia diem
	 * 
	 * @param latlng
	 * @return
	 */
	/*
	 * public Marker getMarkerFromLatLng(LatLng latlng) { for (Marker mk :
	 * listItem) { if (mk.getPosition().equals(latlng)) return mk; } return
	 * null; }
	 */

	/**
	 * 
	 * @param latlng
	 * @param visible
	 */
	public void setVisibility(Marker markerop, boolean visible) {
		// Marker markerop = getMarkerFromLatLng(latlng);
		visibilityMap.put(markerop, visible);
		if (visible) {
			markerop.setIcon(BitmapDescriptorFactory
					.fromResource(R.drawable.pin_blue));
		} else {
			currentItem = null;
			markerop.setIcon(BitmapDescriptorFactory
					.fromResource(R.drawable.pin_invisible));
		}
	}

	public Marker getCurrentItem() {
		return currentItem;
	}

	public Marker getCurrentLocationItem() {
		return currentLocationItem;

	}

	/**
	 * 
	 * @param latlng
	 */
	public void setCurrentLocation(LatLng latlng) {
		if (currentLocationItem == null) {
			String str = context.getString(R.string.MyLocation);
			MarkerOptions mko = new MarkerOptions()
					.icon(BitmapDescriptorFactory
							.fromResource(R.drawable.pin_red));
			mko.position(latlng);
			mko.title(str);
			currentLocationItem = map.addMarker(mko);
			map.animateCamera(CameraUpdateFactory.newLatLng(latlng));
		}
	}

	/*
	 * public boolean getVisibility(LatLng gp) { Marker item =
	 * this.getMarkerFromLatLng(gp); if (item != null &&
	 * visibilityMap.containsKey(item)) { return visibilityMap.get(item); }
	 * return false; }
	 */
	public boolean getVisibility(Marker item) {
		// Marker item = this.getMarkerFromLatLng(gp);
		if (visibilityMap.containsKey(item)) {
			return visibilityMap.get(item);
		}
		return false;
	}

}
